//import org.jetbrains.annotations.Contract;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class LargestSeriesProductCalculator {
    private long[] numbers;

//    @Contract("null -> fail")
    LargestSeriesProductCalculator(String inputNumber) {
        if(null == inputNumber) throw new IllegalArgumentException("String to search must be non-null.");

        if (!isNumeric(inputNumber)) throw new IllegalArgumentException("String to search may only contain digits.");

        if (inputNumber.isEmpty()) {
            this.numbers = new long[0];
            return;
        }

        String[] inputStrings = Objects.requireNonNull(inputNumber).split("");
        this.numbers = new long[inputStrings.length];

        for (int i = 0; i < this.numbers.length; i++) {

            this.numbers[i] = Long.valueOf(inputStrings[i]);
        }
    }

    private boolean isNumeric(String str) {

        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        return isNum.matches();
    }

    long calculateLargestProductForSeriesLength(int numberOfDigits) {
        if (numberOfDigits < 0) {
            throw new IllegalArgumentException("Series length must be non-negative.");
        }

        if (numberOfDigits > this.numbers.length) {
            throw new IllegalArgumentException("Series length must be less than or equal to the length of the string to search.");
        }

        long result = 0;
        for (int i = 0; i < this.numbers.length - numberOfDigits + 1; i++) {
            long t = 1;
            for (int j = 0; j < numberOfDigits; j++) {
                t = t * this.numbers[i + j];
            }

            if (t > result) {
                result = t;
            }
        }

        return result;
    }
}
